package com.flume.demo1;

import org.apache.flume.Context;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.PollableSource;
import org.apache.flume.conf.Configurable;
import org.apache.flume.event.EventBuilder;
import org.apache.flume.source.AbstractSource;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Random;

/**
 * @author shengtao
 * @Description: 自定义resources
 * @date 2019/01/11 18:11
 */
public class CustomSource extends AbstractSource implements Configurable,PollableSource{

    private Context context ;

    @Override
    public void configure(Context context) {
        this.context = context;
    }


    /**
     * 执行
     * @return
     * @throws EventDeliveryException
     */
    @Override
    public Status process() throws EventDeliveryException {
        Random random = new Random();
        int randomNum = random.nextInt(100);
        String text = "Hello world" + random.nextInt(100);
        HashMap<String, String> header = new HashMap<String,String>();
        header.put("id",Integer.toString(randomNum));
        this.getChannelProcessor()
                .processEvent(EventBuilder.withBody(text, Charset.forName("UTF-8"),header));
        return Status.READY;
    }


}
